import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path';
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), '')
  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        'assets': resolve(__dirname, 'src/assets')
      }
    },
    css: {
      postcss: {
        plugins: [
          tailwindcss('./tailwind.config.js'),
          autoprefixer(),
        ],
      },
    },
    server: {
      proxy: {
        '/api': {
          // 代理的目标服务器地址
          target: env.VITE_APP_BASE_API,
          // 路径重写，将匹配到的 '/api' 请求转发到目标服务器，并替换掉 '/api'
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        },
      }
    }
  }
})
